package br.com.sistema.smallAgro.usuario;

import java.util.Arrays;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.sistema.smallAgro.enums.IRenderStatus;
import br.com.sistema.smallAgro.enums.SimNao;
import br.com.sistema.smallAgro.modelo.Usuario;
import br.com.sistema.smallAgro.service.UsuarioService;
import br.com.sistema.smallAgro.util.Constantes;
import br.com.sistema.smallAgro.util.ProviderBase;

public abstract class ConsultaUsuarioForm extends Form<Usuario> {
	private static final long serialVersionUID = 2324933405213908395L;
	private TextField<String> nome;
	RadioChoice<SimNao> status;
	@SpringBean
	private UsuarioService usuarioService;
	private WebMarkupContainer grid;
	private Label nenhumRegistro;
	private Component feedbackError;
	private Component feedback;

	public ConsultaUsuarioForm(String id) {
		super(id, new CompoundPropertyModel<Usuario>(new Usuario()));
		addOrReplace(feedbackError = new FeedbackPanel("msgErro",
				new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR))
				.setOutputMarkupId(true));
		addOrReplace(feedback = new FeedbackPanel("msgSucesso",
				new ExactLevelFeedbackMessageFilter(FeedbackMessage.SUCCESS))
				.setOutputMarkupId(true));

		criaComponentes();
		grid = new WebMarkupContainer("container");
		add(grid);
		grid.setOutputMarkupId(true);

		nenhumRegistro = new Label("nenhumRegistro", Constantes.NENHUM_REGISTRO);
		addOrReplace(nenhumRegistro);

		DataView<Usuario> dataView = new DataView<Usuario>("grid",
				getProvider()) {
			private static final long serialVersionUID = -2991827635504637633L;

			@Override
			protected void populateItem(Item<Usuario> item) {
				item.add(new Label("nome"));
				item.add(new Label("status"));

				criaLinkExcluir(item);
				criaLinkEditar(item);
			}
		};

		dataView.setOutputMarkupId(true);
		nenhumRegistro.setOutputMarkupId(true);

		dataView.setItemsPerPage(5);
		grid.addOrReplace(dataView);

		if (getProvider().size() == 0) {
			grid.setVisible(false);
			nenhumRegistro.setVisible(true);
		} else {
			nenhumRegistro.setVisible(false);
		}
		grid.addOrReplace(new PagingNavigator("navegacao", dataView)
				.setOutputMarkupId(true));
	}

	private ProviderBase<Usuario> getProvider() {
		ProviderBase<Usuario> provider = new ProviderBase<Usuario>(
				getModelObject(), usuarioService);
		return provider;
	}

	private void criaLinkEditar(final Item<Usuario> item) {
		item.add(new Link<Object>("editar") {
			private static final long serialVersionUID = 3790909760895748707L;

			@Override
			public void onClick() {
				setResponsePage(new CadastrarUsuarioPage(item.getModelObject()));
			}
		});
	}

	private void criaLinkExcluir(final Item<Usuario> item) {
		item.add(new Link<Object>("excluir") {
			private static final long serialVersionUID = -6280644292846955187L;

			@Override
			public void onClick() {
				excluirUsuario(item.getModelObject());
				feedback.success(Constantes.DADO_EXCLUIDO);
			}
		});
	}

	private void criaComponentes() {
		add(nome = new TextField<String>("nome"));

		status = new RadioChoice<SimNao>("status", new PropertyModel<SimNao>(
				getModelObject(), "status"), Arrays.asList(SimNao.values()),
				new IRenderStatus());
		add(status);

		add(new AjaxButton("pesquisar") {
			private static final long serialVersionUID = 656384056916149792L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				if (isPreenchido()) {
					getProvider().setFiltro(getModelObject());
				} else {
					feedbackError.error(Constantes.DADO_OBRIGATORIO);
				}
				target.add(grid);
			}

			private boolean isPreenchido() {
				boolean preenchido = false;
				if (status.getModelObject() != null) {
					preenchido = true;
				}
				if (nome.getModelObject() != null) {
					preenchido = true;
				}
				return preenchido;
			}
		});

		add(new Button("limpar") {
			private static final long serialVersionUID = 860225218339979017L;

			@Override
			public void onSubmit() {
				setResponsePage(new ConsultarUsuarioPage());
			}
		});

		addOrReplace(new Link<Void>("usuarioNovo") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new CadastrarUsuarioPage(new Usuario()));
			}

		});
	}

	public abstract void excluirUsuario(Usuario usuario);
}